dictFile = open('dict.txt', 'r')
dictList = {}
for word in dictFile.readlines():
	if dictList.has_key(word[0]):
		dictList[word[0]].append(word.rstrip())
	else:
		dictList[word[0]] = [word.rstrip()]
dictList['2'] = ['hey']

print "Finished creating the list of all words in dictionary"

l1 = 'a'
l2 = 't'
l3 = 'w'
l4 = 'g'
l5 = 'n'
l6 = 'm'
l7 = 'c'
l8 = 'p'

letterlist = [l1, l2, l3, l4, l5, l6, l7, l8]

wordslist = []

for x1 in range(0, 8):
	wordslist.append(letterlist[x1])
	for x2 in range(0, 8):
		print '1'
		if x1 != x2:
			theword = letterlist[x1] + letterlist[x2]
			if theword not in wordslist:
				wordslist.append(theword)
			for x3 in range(0, 8):
				repeatlist = [x1, x2, x3]
				if len(repeatlist) == len(set(repeatlist)):
					theword = letterlist[x1] + letterlist[x2] + letterlist[x3]
					if theword not in wordslist:
						wordslist.append(theword)
					for x4 in range(0, 8):
						repeatlist = [x1, x2, x3, x4]
						if len(repeatlist) == len(set(repeatlist)):
							theword = letterlist[x1] + letterlist[x2] + letterlist[x3] + letterlist[x4]
							if theword not in wordslist:
								wordslist.append(theword)
							for x5 in range(0, 8):
								repeatlist = [x1, x2, x3, x4, x5]
								if len(repeatlist) == len(set(repeatlist)):
									theword = letterlist[x1] + letterlist[x2] + letterlist[x3] + letterlist[x4] + letterlist[x5]
									if theword not in wordslist:
										wordslist.append(theword)			
									for x6 in range(0, 8):
										repeatlist = [x1, x2, x3, x4, x5, x6]
										if len(repeatlist) == len(set(repeatlist)):
											theword = letterlist[x1] + letterlist[x2] + letterlist[x3] + letterlist[x4] + letterlist[x5] + letterlist[x6]
											if theword not in wordslist:
												wordslist.append(theword)
											for x7 in range(0, 8):
												repeatlist = [x1, x2, x3, x4, x5, x6, x7]
												if len(repeatlist) == len(set(repeatlist)):
													theword = letterlist[x1] + letterlist[x2] + letterlist[x3] + letterlist[x4] + letterlist[x5] + letterlist[x6] + letterlist[x7]
													if theword not in wordslist:
														wordslist.append(theword)
													for x8 in range(0, 8):
														repeatlist = [x1, x2, x3, x4, x5, x6, x7, x8]
														if len(repeatlist) == len(set(repeatlist)):
															theword = letterlist[x1] + letterlist[x2] + letterlist[x3] + letterlist[x4] + \
																		letterlist[x5] + letterlist[x6] + letterlist[x7] + letterlist[x8]
															if theword not in wordslist:
																wordslist.append(theword)

print "Finished forming sequence of words"

realList = []
for realword in wordslist:
	if dictList[realword[0]].count(realword) > 0:
		realList.append(realword)
		print realword 

print "Finished showing realwords. Best options:"

for word in realList:
	if len(word) == 8 and word.count(l8) > 0:
		print word

for word in realList:
	if len(word) == 7 and word.count(l8) > 0:
		print word

for word in realList:
	if len(word) == 6 and word.count(l8) > 0:
		print word

for word in realList:
	if len(word) == 5 and word.count(l8) > 0:
		print word

for word in realList:
	if len(word) == 4 and word.count(l8) > 0:
		print word
